# !/usr/local/bin/ruby -w 
# authenticate.rb
#
# This program realizes the AdministratorInterface Subsystem.
# It gives the Administrator access to View Database after authentication.
# The Administrator is given privilage to view database based on selected parameters
#
# Authors:: Ronak Shah, Akshay Jog (mailto: {ronshah,akshayj}@eden.rutgers.edu )
# License:: Distributes under the same terms as Ruby
#
# This class has method check () to authenticate the administrator by checking 
# its login details (username and password)

class Authenticate

    attr_accessor :username, :password

# Remembers the initial username and password
    def initialize(username, password)
       @username = username
       @password = password

    end

# Method to check to see if the the input login details are same as those stored
    def check(user)

require "mysql"

dbname = "tripti"

# Connect to the database
m = Mysql.new("idb1", "orbit", "orbit")
# r = m.query("CREATE DATABASE #{dbname}")

# Select the database to be used
m.select_db(dbname)


       @username= @username.strip!
       @password= @password.strip!

       if (@username!= user.username || @password!=user.password )
         puts "Invalid username or password"
       else
         puts "Welcome"

# Privilage given to Administrator to view Database based on given Parameters
         puts "Please Enter one of the following options to view enteries in Database \n
               1. To view enteries less than particular time stamp: Enter time as hh:mm:ss < \n
               2. To view enteries greater than particular time stamp: Enter time as hh:mm:ss > \n
               3. To view enteries at that particulas time stamp: Enter time as hh:mm:ss = \n
               4. To view enteries of a particular sender IP address: Enter 192.168.x.y sender \n
               5. To view enteries of a particular receiver IP address: Enter 192.168.x.y receiver \n"

         str=gets
         puts str
           temp = []
           temp = str.split(" ")

	case str

             when /\d\d:\d\d:\d\d </
                puts "You have requested to view DB less than this time stamp"
                #Query for DB as per time stamp
		result = m.query("SELECT * FROM packet WHERE timestamp < '#{temp[0]}';")
		result.each do |row|
                row.each do |column|
		puts " #{column} "
                  end
		puts
		end

	     when /\d\d:\d\d:\d\d >/
                puts "You have requested this input to view DB greater than this time"
                #Query for DB as per time stamp
                result =  m.query("SELECT * FROM packet WHERE timestamp > '#{temp[0]}';")
                result.each do |row|
                row.each do |column|
                puts " #{column} "
                end
		puts
		end


	    when /\d\d:\d\d:\d\d =/
                puts "You have requested to view db as per time equal to t"
                #Query for db as per time equal to given time
                result = m.query("SELECT * FROM packet WHERE timestamp = '#{temp[0]}';")
	        result.each do |row|
	        row.each do |column|
	        puts " #{column} "
	        end
		puts
		end

	     when /192.168.*.* receiver/
                puts "Receiver case"
                #Query for db as per receiver IP
                result =  m.query("SELECT * FROM packet WHERE dst_addr = '#{temp[0]}';")
                result.each do |row|
                row.each do |column|
                puts " #{column} "
                end
                puts
		end


	     when /192.168.*.* sender/
                puts "Sender case"
                #Query for db as per sender IP
                result =  m.query("SELECT * FROM packet WHERE src_addr = '#{temp[0]}';")
                result.each do |row|
                row.each do |column|
                puts " #{column} "
		end
                puts
		end

             else
                puts "Please try again and provide valid input"

             end   #end of case

          end

	  #Close the connection to database
	  m.close

    end # end of def

end #end of class


# Administrator Login Details: Username: admin, Password: star
user = Authenticate.new("admin", "star")

#Enter Login Details
user_check=Authenticate.new("a","b")
puts "Enter Username"
user_check.username=gets
puts "Enter Password"
user_check.password=gets
user_check.check(user)

